package com.lanlian.cayman.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.bean.CMinute;
import com.example.bean.FenshiDataResponse;
import com.example.bean.StickData;
import com.example.myview.MsgView;
import com.example.utils.LineUtil;
import com.lanlian.cayman.HqMsg;
import com.lanlian.cayman.R;
import com.lanlian.cayman.bean.EventZixuanData;
import com.lanlian.cayman.bean.GearBean;
import com.lanlian.cayman.bean.MyContractBean;
import com.lanlian.cayman.dialog.DrawLinePopup;
import com.lanlian.cayman.dialog.GoodsSelectDialog;
import com.lanlian.cayman.dialog.KlineSelectDialog;
import com.lanlian.cayman.dialog.PlaceOrderDialogManager;
import com.lanlian.cayman.fragment.MarketFragment;
import com.lanlian.cayman.kline.utils.MyDataParse;
import com.lanlian.cayman.proto.ProtoManager;
import com.lanlian.cayman.socket.MySocketManager;
import com.lanlian.cayman.socket.SocketCallImpl;
import com.lanlian.cayman.utils.CLOrientationDetector;
import com.lanlian.cayman.utils.KlneUtil;
import com.lanlian.cayman.utils.MResUtil;
import com.lanlian.cayman.utils.NumberManager;
import com.lanlian.cayman.utils.TimeUtil;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.cayman.view.RiskInvestigationDialog;
import com.lanlian.cayman.widget.TopView;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.ReqGetTradeTime;
import com.lanlian.commonlib.bean.db.DBMarketS;
import com.lanlian.commonlib.common.SystemConfig;
import com.lanlian.commonlib.dao.DBManager;
import com.lanlian.commonlib.data.RespTradeTime;
import com.lanlian.commonlib.event.EventCode;
import com.lanlian.commonlib.event.EventMessage;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.commonlib.util.EventBusUtils;
import com.lanlian.uikit.CustomDialog;
import com.oms.rpc.protobuf.KLineHqResp;
import com.oms.rpc.protobuf.OMSTradeReq;
import com.oms.rpc.protobuf.OMSTradeRsp;
import com.oms.rpc.protobuf.TickHqReq;
import com.oms.rpc.protobuf.TickHqResp;
import com.oms.rpc.protobuf.YsHqInfo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

/**
 * <pre>
 *     date   : 2018/8/14 14:25
 *     desc   : k线横屏
 *     version: 1.0
 * </pre>
 */
public class KLineHengPingActivity extends KLineActivityBase  {


    Activity act=this;
   public DrawLinePopup popupWindow;

    ImageView iv_more;
    PlaceOrderDialogManager pm;
    public DBManager mDBManager;
    HqMsg.YsHqInfo yhc;
    RespTradeTime mRespTradeTime;
    ArrayList<GearBean> buy;

   ImageView iv_close;
   TextView tv_xiadan;
    private ArrayList<StickData> list;
   TextView tv_add;

    String scale="0.01";
    FenshiDataResponse fd;
    ImageView iv_search;
    TextView tv_price;
    TextView tv_change;
    TextView tv_chg;
    /**
     * 交易时间
     */

    String time=null;
    TextView tv_cangchi;

    CLOrientationDetector cl;
    LinearLayout ll_bottom;
    LinearLayout ll_more;
    private long longTime;
    private long tickTime;
    int count;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

                WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题
        super.onCreate(savedInstanceState);

        longTime = System.currentTimeMillis();

        cl=new CLOrientationDetector(this);
        cl.setIcb(new CLOrientationDetector.Icb() {
            @Override
            public void back(boolean isPoart) {
                if(isPoart)
                {
                    finish();
                    Log.e("test2","4");
                }
                else
                {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                    Log.e("test2","5");
                }
            }
        });


        setContentView(R.layout.activity_klinehengping);

        name=getIntent().getStringExtra("name");
        id=getIntent().getStringExtra("id");
//        klineType=getIntent().getIntExtra("index",0);
//        selectType=getIntent().getIntExtra("selectType",0);

        mDBManager = DBManager.getInstance();

        initTop(KLineActivity.selectType);
        reData();




    }

    @Override
    protected void setRequestedOrientation() {
        //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//横屏
    }

    @Override
    protected void initView() {
        super.initView();
        tv_price=findViewById(R.id.tv_price);
        tv_change=findViewById(R.id.tv_change);
        tv_chg=findViewById(R.id.tv_chg);

        ll_bottom=findViewById(R.id.ll_bottom);
        ll_more=findViewById(R.id.ll_more);
        iv_more=findViewById(R.id.iv_more);
        ll_more.setOnClickListener(this);
        tv_ma=findViewById(R.id.tv_ma);
        tv_ema=findViewById(R.id.tv_ema);
        tv_boll=findViewById(R.id.tv_boll);
        tv_vol=findViewById(R.id.tv_vol);
        tv_macd=findViewById(R.id.tv_macd);
        tv_kdj=findViewById(R.id.tv_kdj);
        tv_rsi=findViewById(R.id.tv_rsi);
        tv_roc=findViewById(R.id.tv_roc);
        tv_ma.setOnClickListener(this);
        tv_ema.setOnClickListener(this);
        tv_boll.setOnClickListener(this);
        tv_vol.setOnClickListener(this);
        tv_macd.setOnClickListener(this);
        tv_kdj.setOnClickListener(this);
        tv_rsi.setOnClickListener(this);
        tv_roc.setOnClickListener(this);


        iv_search=findViewById(R.id.iv_search);
        iv_search.setOnClickListener(this);
        iv_close=findViewById(R.id.iv_close);
        iv_close.setOnClickListener(this);
        tv_cangchi=findViewById(R.id.tv_cangchi);
        tv_xiadan=findViewById(R.id.tv_xiadan);
        tv_xiadan.setOnClickListener(this);

        tv_add=findViewById(R.id.tv_add);
        tv_add.setOnClickListener(this);



        MsgView ll_tp=findViewById(R.id.ll_top);
        kline=findViewById(R.id.kline);
        kline.setMsgLL(ll_tp);

        TextView tv_name=(TextView) findViewById(R.id.tv_name);
        tv_name.setText(name);


        if(DBMarketS.isHas(id))
        {
            tv_add.setText("删自选");
        }else
        {
            tv_add.setText("加自选");
        }
        kline.setOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(view==null)
                {

                }
                else
                {
                    nextZhiBiao();
                }

            }
        });
    }


    public void reData()
    {
        try {

            initView();

            scale= MarketFragment.getRespMarket().getGoods(id).minChange;
            int count=NumberManager.getNumberDecimalDigits(scale);
            kline.setScalePrice(count);
            initDataWuDang();

            getTradeTime();
            if(selectType==0)
            {
                initFenShiData();
            }
            else
            {
                initKlneData(selectType);
            }

            kline.setType(klineType,zhibiaoTop);



        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    /**
     * 获取交易时间
     */
    private void getTradeTime()
    {
        ReqGetTradeTime rm=new ReqGetTradeTime();
        VolleyRequestUtils.getInstance().sendRequest(this.toString(), rm, new INetWorkCallback() {
            @Override
            public void onSuccess(Object response) {
                mRespTradeTime=(RespTradeTime)response;

                String goodsid=MarketFragment.getRespMarket().goodsId(id);

//            GoodsFragment goodsf=(GoodsFragment) mFragments.get(1);
//            goodsf.setTime(mRespTradeTime.getTime(goodsid));
                time=mRespTradeTime.getTime(goodsid);
                setFenShiData();
                setKlineData();

            }

            @Override
            public void onFail(BaseResponse baseResponse) {

            }
        });
    }
    private void initDataWuDang()
    {
        ProtoManager pm=new ProtoManager();
        try {
            TickHqReq.Builder builder = pm.getTickHqReq();
            builder.setStrcontractno(id);
            //下面的方法必须最后调用
            OMSTradeReq.Builder oms=pm.getRequst(6);
            OMSTradeReq om=oms.build();
            SocketCallImpl sc=new SocketCallImpl(this){
                @Override
                public void message(byte[] data) {
                    try
                    {
                        OMSTradeRsp oms=  OMSTradeRsp.parseFrom(data);
                        TickHqResp hr= oms.getTickhqResp();
                        YsHqInfo yh=hr.getTickhqinfo(0);

                        int count= NumberManager.getNumberDecimalDigits(scale);

                        MyContractBean mb=MyContractBean.switchData(yh,count);
//                        GearFragment gf=(GearFragment) mFragments.get(0);
//                        if(mb!=null)
//                        {
//                            gf.setData(mb);
                            setTop(mb,null);
                        kline.setTop(mb);
//                        }



                        buy = new ArrayList<>();
                        ArrayList<GearBean> sell = new ArrayList<>();


                        GearBean gb = null;
                        for (int i = 0; i < 5; i++) {
                            gb = new GearBean();
                            gb.id = 1 + i + "";
                            gb.price = NumberManager.getString4or5(yh.getQBidPrice(i) + "", "1", count);
                            gb.num = yh.getQBidQty(i) + "";

                            buy.add(gb);
                        }
                        for (int i = 0; i < 5; i++) {
                            gb = new GearBean();
                            gb.id = 1 + i + "";

                            gb.price = NumberManager.getString4or5(yh.getQAskPrice(i) + "", "1", count);
                            gb.num = yh.getQAskQty(i) + "";
                            sell.add(gb);
                        }

                        kline.setLastPrice(mb.ClosingPrice);
                        kline.setGearData(buy, sell);

                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            };
            MySocketManager ms=new MySocketManager();
            ms.setCongLian(true);
            ms.sendData(om.toByteArray(),sc);
        }catch (Exception e)
        {
            e.printStackTrace();
        }

    }
    private void initKlneData(int index)
    {
        selectType=index;
        if(index==0)
        {
            KLineActivity.klineType=0;
            initFenShiData();
            return;
        }
        else if(index==1)
        {
            KLineActivity.klineType=1;

        }
        else if(index==2)
        {
            KLineActivity.klineType=5;

        }
        else if(index==3)
        {
            KLineActivity.klineType=10;

        }
        else if(index==4)
        {
            KLineActivity.klineType=30;

        }
        else if(index==5)
        {
            KLineActivity.klineType=60;

        }
        else if(index==6)
        {
            KLineActivity.klineType=24;

        }

        KlneUtil ku=new KlneUtil();
        ku.getKline(this, id, KLineActivity.klineType+"", new KlneUtil.IcbCall() {
            @Override
            public void comple(Object obj) {
                KLineHqResp resp=(KLineHqResp)obj;
                list=new ArrayList<>();
                StickData sd=null;
                String s1=null;
                for(int i=0;i<resp.getKlineinfoCount();i++)
                {
                    if(i==198)
                    {
                        int a=1;
                    }
                    s1=resp.getKlineinfo(resp.getKlineinfoCount()-1-i).getStrkline();
                    if(s1!=null&&!s1.equals(""))
                    {
                        sd=MyDataParse.parse2(s1,klineType);
                        list.add(sd);
                    }


                }

                setKlineData();
            }
        });
    }
    private void initFenShiData()
    {
        showLoadingDialog();
        KlneUtil ku=new KlneUtil();
        ku.getFenshi(this, id, null, new KlneUtil.IcbCall() {
            @Override
            public void comple(Object obj) {
                KLineHqResp resp=(KLineHqResp)obj;
                String s1=null;

                s1=resp.getKlineinfo(0).getStrkline();

                fd= MyDataParse.parseFenShi(s1);
                setFenShiData();
//                   String s= TimeUtil.TimeStamp2Date(fd.getData().get(0).getTime()+"");
//                Log.e("fen",fd.getData().get(0).price+": "+s);


            }
        });

    }
    private void setKlineData()
    {

        if(time!=null&&list!=null&&selectType!=0&&list.size()>0)
        {
            list.get(0).time_jiaoyo=time;
            kline.setData(list,klineType);
            if(list.size()>=2)
            {

                long min1= LineUtil.getMin(list.get(list.size()-2).getTime());
                long min2= LineUtil.getMin(list.get(list.size()-1).getTime());
                long start= LineUtil.countTime(time,list.get(list.size()-1).getTime(),klineType);
                long yu=(min2)%klineType;
//                if(min1==min2||(LineUtil.getMin(start) - min2) % klineType != 0)
//                {
//                    list.get(list.size()-1).setTime(start);
//                }
                if(min1==min2)
                {
                    list.get(list.size()-1).setTime(start);
                }
                else
                {
                    if((start-list.get(list.size()-1).getTime())% (klineType*60)!= 0)
                    {
                        list.get(list.size()-1).setTime(start);
                    }
                }

            }

            reZhibiao();
        }
    }
    private void setFenShiData()
    {
        if(time!=null&&fd!=null&&selectType==0)
        {
            String s1=getCangChi();
            if(s1!=null)
            {
                tv_cangchi.setText("场次"+getCangChi());
            }

            fd.getParam().setDuration(time);
            count=NumberManager.getNumberDecimalDigits(scale);
            kline.setScalePrice(count);
            kline.setFenshiData(fd);
            //reZhibiao();
        }


    }

    /**
     * 得到当前场次
     * @return
     */
    public  String getCangChi()
    {
        String cdate="";
        String cangchi=null;

        Date date=new Date();
        String formats   = "HH:mm";

        cdate = new SimpleDateFormat(formats, Locale.CHINA).format(new Date());

        if(time==null)
        {
            return "--";
        }
        ArrayList<String> times = new ArrayList<>();
        if (time.contains("|")) {
            String[] t1 = time.split("\\|");
            for (String s : t1) {
                times.add(s.split("-")[0]);
                times.add(s.split("-")[1]);
            }

        } else {
            times.add(time.split("-")[0]);
            times.add(time.split("-")[1]);
        }

        ArrayList<Integer> mins = new ArrayList<>();
        for(String minStr : times) {

            int min = Integer.parseInt(minStr.split(":")[0]) * 60 + Integer.parseInt(minStr.split(":")[1]);
            mins.add(min);
        }
        int cmin= Integer.parseInt(cdate.split(":")[0]) * 60 + Integer.parseInt(cdate.split(":")[1]);
        for(int i=0;i<mins.size();i=i+2)
        {
            int a=mins.get(i);
            int b=mins.get(i+1);
            if(cmin>=a&&cmin<=b)
            {
                cangchi=times.get(i)+"-"+times.get(i+1);
            }
        }
        return  cangchi;
    }
    @Override
    public void onClick(View v) {
        super.onClick(v);
        Intent intent=new Intent();
        switch (v.getId()) {
            case R.id.iv_search:
                GoodsSelectDialog gsd=new GoodsSelectDialog(this);
                gsd.showDialog();
                break;
            case R.id.iv_close:
                if (System.currentTimeMillis() - longTime > 3000) {
                    setItem(-1);
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                }
                break;
            case R.id.ll_more:
                KlineSelectDialog dialog=new KlineSelectDialog(this);
                dialog.showDialog(ll_bottom);

                break;
            case R.id.tv_add:
                if(DBMarketS.isHas(id)) {
                    mDBManager.deleteMarketS(id);
                    tv_add.setText("加自选");
                    EventBusUtils.post(new EventMessage<>(EventCode.EVENT_ZIXUAN_REFRESH,
                            new EventZixuanData(new MyContractBean(id, name), false)));
                } else {
                    DBMarketS ms=new DBMarketS();
                    ms.setId(id);
                    ms.setName(name);
                    mDBManager.setMarketS(ms);
                    tv_add.setText("删自选");
                    EventBusUtils.post(new EventMessage<>(EventCode.EVENT_ZIXUAN_REFRESH,
                            new EventZixuanData(new MyContractBean(id, name), true)));
                }
                break;
            case R.id.tv_xiadan:
                if(!mDBManager.isLogined())
                {

                    intent.setClass(this, LoginActivity.class);
                    startActivity(intent);
                    return;
                }
                if(mDBManager.getUser() != null &&
                        mDBManager.getUser().getRiskAuthState() == 0 &&
                        mDBManager.getIsOldUser() == 0) {
                    new RiskInvestigationDialog(this, new RiskInvestigationDialog.OnClickListener() {
                        @Override
                        public void onWriteClick() {
                            startActivity(new Intent(KLineHengPingActivity.this, RiskStateActivity.class)
                                    .putExtra("mobile", mDBManager.getUser().getMobileNo()));
                        }

                        @Override
                        public void onCloseClick() {
                        }
                    }).show();
                    return;
                }
                if(pm!=null&&pm.isShow())
                {
                    return;
                }
                pm=new PlaceOrderDialogManager(this);
                pm.showDialog2();
                if(buy!=null&&buy.size()>0)
                {
                    pm.setData(id,name,tv_price.getText().toString());
                }
                else
                {
                    pm.setData(id,name,null);
                }
                break;



            default:
                break;
        }
    }
    //为了解决弹出PopupWindow后外部的事件不会分发,既外部的界面不可以点击
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
//        if (popupWindow != null && popupWindow.isShowing()) {
//            return false;
//        }
            return super.dispatchTouchEvent(event);


    }
    private void initTop(int index)
    {
        TopView topv=findViewById(R.id.topv);

        ArrayList<String> top=new ArrayList<>();
        top.add("分时");
        top.add("1分钟");
        top.add("5分钟");
        top.add("10分钟");
        top.add("30分钟");
        top.add("1小时");
        top.add("日k");
        TopView.onClickCall call=new TopView.onClickCall() {
            @Override
            public void back(int index) {

                kline.setType(index,zhibiaoTop);
                initKlneData(index);
            }
        };
        topv.setSelectColor(getResources().getColor(R.color.color_text_style4));
        topv.setData(top,call,index);
    }
    @Override
    protected boolean isRegisteredEventBus() {
        return true;
    }
    @Override
    public void onReceiveEvent(EventMessage event) {
        super.onReceiveEvent(event);
        switch (event.getCode()){
            case EventCode.EVENT_MARKET:
                try {
                    if(!isForeground(act,act.getClass().getName()))
                    {
                        return;
                    }
                    HqMsg.YsHqInfo yh = (HqMsg.YsHqInfo) event.getData();
                    String s1=yh.getCommodityNo()+yh.getContractNo1();
                    if(!id.equals(s1))
                    {
                        return;
                    }
                    yhc=yh;

                    // GearFragment gf=(GearFragment) mFragments.get(0);
                    // gf.setData(yh);


                    long t = TimeUtil.Date2TimeStamp2(yh.getTAPIDTSTAMP(), null);
                    if (t == tickTime && yh.getQLastQty() == 0) {
                        return;
                    }
                    tickTime = t;

                    MyContractBean mb=MyContractBean.switchData(yhc,count);
//                    if(mb.vol>0) {
//                        Log.e("test2", mb.time_test + ": " + yh.getTAPIDTSTAMP() + "" + ":" + mb.vol);
//                    }
                   // GearFragment gf=(GearFragment) mFragments.get(0);


                    if( KLineActivity.klineType!=0) {
                        upDateKline(mb);
                    }
                    if(mb!=null)
                    {
                       // gf.setData(mb);
                        setTop(mb,null);
                        kline.setTop(mb);
                    }

                    if(KLineActivity.klineType==0) {
                        updateFenshi(mb);

                        buy = new ArrayList<>();
                        ArrayList<GearBean> sell = new ArrayList<>();


                        GearBean gb = null;
                        for (int i = 0; i < 5; i++) {
                            gb = new GearBean();
                            gb.id = 1 + i + "";
                            gb.price = NumberManager.getString4or5(yh.getQBidPrice(i) + "", "1", count);
                            gb.num = yh.getQBidQty(i) + "";

                            buy.add(gb);

                            gb = new GearBean();
                            gb.id = 1 + i + "";

                            gb.price = NumberManager.getString4or5(yh.getQAskPrice(i) + "", "1", count);
                            gb.num = yh.getQAskQty(i) + "";
                            sell.add(gb);
                        }

//                        if (buy.size() > 1 && pm != null) {
//                            pm.setPrice(buy.get(0).price);
//                        }
                        kline.setLastPrice(mb.ClosingPrice);
                        kline.setGearData(buy, sell);
                    }

                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
                break;
            case EventCode.EVENT_MESSAGE_NETWORK:
                reData();
                break;
            case EventCode.EVENT_LOGIN_REFRESH:
                if (mDBManager.getIsOldUser() == 1) {
                    new CustomDialog(this, new CustomDialog.OnClickListener() {
                        @Override
                        public void onLeftClick() {
                        }

                        @Override
                        public void onRightClick() {
                            startActivity(new Intent(KLineHengPingActivity.this, UpdateRegisterAgreementActivity.class)
                                    .putExtra("mobile", mDBManager.getUser().getMobileNo()));
                        }
                    }).setContent("尊敬的客户，为了给您提供更完善的交易服务，请您完善账户资料补充申请（通讯地址、资产职业信息），感谢您的配合！")
                            .setLeft("稍后").setRight("进入").setGravity(Gravity.LEFT).show();
                } else if (mDBManager.getUser().getRiskAuthState() == 0) {
                    new RiskInvestigationDialog(this, new RiskInvestigationDialog.OnClickListener() {
                        @Override
                        public void onWriteClick() {
                            startActivity(new Intent(KLineHengPingActivity.this, RiskStateActivity.class)
                                    .putExtra("mobile", mDBManager.getUser().getMobileNo()));
                        }

                        @Override
                        public void onCloseClick() {
                        }
                    }).show();
                }
                break;
            default:
                break;
        }
    }
    private void setTop(MyContractBean mb,String name)
    {

        if(mb!=null) {
            tv_price.setText(mb.last);
            tv_change.setText(mb.change);
            tv_chg.setText(mb.chg);
            if(NumberManager.isDaYu(mb.change,"0")>0)
            {
                tv_price.setTextColor(MResUtil.getUpColor());
                tv_change.setTextColor(MResUtil.getUpColor());
                tv_chg.setTextColor(MResUtil.getUpColor());
                if(mb.change.contains("+"))
                {
                    tv_change.setText(mb.change);
                }
                else
                {
                    tv_change.setText("+"+mb.change);
                }
                if(mb.chg.contains("+"))
                {
                    tv_chg.setText(mb.chg);
                }
                else
                {
                    tv_chg.setText("+"+mb.chg);
                }
            }
            else if(NumberManager.isDaYu(mb.change,"0")==0)
            {
                tv_price.setTextColor(MResUtil.getPingColor());
                tv_change.setTextColor(MResUtil.getPingColor());
                tv_chg.setTextColor(MResUtil.getPingColor());
            }
            else
            {
                tv_price.setTextColor(MResUtil.getDownColor());
                tv_change.setTextColor(MResUtil.getDownColor());
                tv_chg.setTextColor(MResUtil.getDownColor());
                if(mb.change.contains("-"))
                {
                    tv_change.setText(mb.change);
                }
                else
                {
                    tv_change.setText("-"+mb.change);
                }
                if(mb.chg.contains("-"))
                {
                    tv_chg.setText(mb.chg);
                }
                else
                {
                    tv_chg.setText("-"+mb.chg);
                }
            }
        }
    }
    /**
     * 跟新分时数据
     */
    private void updateFenshi(MyContractBean mb)
    {
        if(KLineActivity.klineType!=0)
        {
            return;
        }
        if(fd==null||fd.getData()==null||fd.getData().size()==0)
        {
            return;
        }
        CMinute last=fd.getData().get(fd.getData().size()-1);
//        if(NumberManager.isDaYu(last.price+"",mb.last)==0)
//        {
//            return;
//        }
        CMinute cm=new CMinute();
        if(!mb.last.equals(""))
        {
            cm.setPrice(Double.parseDouble(mb.last));
        }

        // String val= NumberUtil2.getSubString(mb.TotalNum,fd.getTotalVal(),0);
        if(mb.vol<0)
        {
            mb.vol=0;
        }
        cm.setCount(mb.vol);
        cm.setChange(mb.change);
        cm.setChg(mb.chg);
        cm.setRate(Double.parseDouble(mb.chg.substring(0,mb.chg.indexOf("%"))));
        cm.setTime(mb.time);
        cm.setAverage(mb.Average);
        //fd.addData(cm);
        //setFenShiData();
        kline.updateFenshiData(cm);
    }
    private void upDateKline(MyContractBean mb)
    {
        if(mb.vol<1)
        {
            return;
        }
        StickData sd=new StickData();
        sd.setClose(Double.parseDouble(mb.last));

        sd.setHigh(Float.parseFloat(mb.last));
        //sd.setLast(Double.parseDouble(mb.last));
        sd.setLow(Double.parseDouble(mb.last));


        sd.setOpen(Double.parseDouble(mb.last));


        sd.setCount_total(Double.parseDouble(mb.TotalNum));
        sd.setCount(mb.vol);
        sd.setTime(mb.time);
        kline.updateKlineData(sd);
    }

    private int item;

    public int getItem() {
        return item;
    }

    public void setItem(int item) {
        this.item = item;
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        if (getItem() == -1) {
            finish();
        } else {
            startActivity(new Intent(this, MainActivity.class)
                    .putExtra("item", getItem()));
            finish();
        }
//        Log.e("test2","1");
//        if(newConfig.orientation== Configuration.ORIENTATION_PORTRAIT)
//        {
//            finish();
//            Log.e("test2","2");
//        }
//        else
//        {
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//横屏
//            Log.e("test2","3");
//        }

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        initView();
        initTop(selectType);
        reZhibiao();
        if(selectType==0)
        {
            initFenShiData();
        }
        else
        {
            initKlneData(selectType);
        }
        int orientation = getResources().getConfiguration().orientation;
        if (orientation != Configuration.ORIENTATION_LANDSCAPE)
        {

            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    }
}
